package com.ruoyi.web.controller.ToThirdPart;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/ToThirdPart")
public class toThirdPartGetAuthJHaveToken {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysLoginService loginService;

    @PostMapping("/toThirdPartGetAuthJHaveToken")
    @ApiOperation(value = "平台带着token过来登录")
    public AjaxResult toThirdPartGetAuthJHaveToken(String token) {
        System.out.println("this");
        //调用验证token的方法
        //JSONObject jsonObject = checkJToken(token);
        //String code = jsonObject.getString("code");
        //System.out.println("code :"+code);
        Integer level = 0;
        String loginName = "";
        Long organId = null;
        //返回结果
        AjaxResult ajax = null;
        //真实条件应该为鉴权结果
        if (true) {
            //验证成功
            //JSONObject dataObject = jsonObject.getJSONObject("data");
            //拿到其他的信息
            //level = dataObject.getInteger("level");
            loginName = "T10086";
            organId = Long.valueOf(3);
        } else {
            ajax = AjaxResult.error("404");
            return ajax;
        }

        String isUserNameHas = "T10086";
        String password="123456";
        //检测一下用户名存在不存在
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(loginName))) {
            isUserNameHas = "用户已存在，不需要执行添加的操作";
        } else {
            //用户不存在时，将用户添加到数据库中
            SysUser sysUser = new SysUser();
            //登录名
            sysUser.setUserName(loginName);
            //昵称
            sysUser.setNickName(loginName);
            //密码统一都是123456
            sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
            //创建者，标识J平台过来的用户
            sysUser.setCreateBy("j_have_token");
            //创建日期
            sysUser.setCreateTime(new Date());
            //状态
            sysUser.setStatus("0");
            //所属等级
            //sysUser.setHierarchy(level);
            //明文
            //sysUser.setMingwen("123456");
            //账户权限：为了区分是平台的用户还是本系统用户

            //角色id
            sysUser.setRoleId(Long.valueOf(3));

            //id返回来之后需要加上
            sysUser.setDeptId(organId);

            //所属等级如果没有，则角色是全国的
            //1  省  2 市     3  区
            if (level == null) {
                //角色
                Long[] roleids = {104L};
                sysUser.setRoleIds(roleids);
            } else {
                Long[] roleids = {100L};
                sysUser.setRoleIds(roleids);
            }
            int rows = userService.insertUser(sysUser);
            if (rows > 0) {
                isUserNameHas = "添加成功";
            }
        }
        ajax = AjaxResult.success();
        // 生成令牌(不加验证码登录)
        String tokenNew = loginService.loginNoCode(loginName, "123456", null);
        ajax.put(Constants.TOKEN, tokenNew);
        ajax.put("isUserNameHas", isUserNameHas);
        ajax.put("msg", "登录成功");
        return ajax;
    }


    public JSONObject checkJToken(String token) {
        JSONObject jsonObject = new JSONObject();
        //测试环境
        /*String baseUrl = "http://xxxxx/checkTokenRtnInfo?stk=" + token;

        HttpRequest request = HttpRequest.get("http://xxxxx/checkTokenRtnInfo?stk=",true);

        return (JSONObject) JSONObject.parseObject(request.body().toString());*/
        return null;
    }



}
